﻿using System;
using System.Windows;
using JustinAngel.Neurons.Framework;
using JustinAngel.Neurons.Framework.Messages;
using JustinAngel.Neurons.Framework.Navigation;
using Microsoft.Phone.Shell;

namespace JustinAngel.Neurons.Pages.WebBrowser
{
    public class WebBrowserViewModel : VViewModelBase
    {
        public WebBrowserViewModel()
        {
            IoC.BuildUp(this);


            MessengerInstance.Register<ApplicationDeactivatedMessage>(this, OnApplicationDecativated);
            CheckForApplicationActivated();
        }

        private void CheckForApplicationActivated()
        {
            if (!PhoneApplicationService.Current.State.ContainsKey("WebBrowserViewModel.Data"))
                return;

            var data = (Uri)PhoneApplicationService.Current.State["WebBrowserViewModel.Data"];
            if (data != null)
            {
                Data = data;
                PhoneApplicationService.Current.State.Remove("WebBrowserViewModel.Data");
            }


        }

        private void OnApplicationDecativated(ApplicationDeactivatedMessage obj)
        {
            PhoneApplicationService.Current.State["WebBrowserViewModel.Data"] = Data;
        }

        public override void OnDataSet()
        {
            CurrentSource = RealData;
        }

        public string twitterSuccessfulShareUrl = "http://m.twitter.com/share/complete";
        public string twitterLoginFailUri = "res://ieframe.dll/http_default.htm#http://m.twitter.com/share";
        public string facebookSuccessfulShareUrl = "http://m.facebook.com/profile.php?posted_id=";

        public Uri RealData { get { return (Uri) Data; }}

        private Uri _currentSource;
        public Uri CurrentSource
        {
            get { return _currentSource; }
            set
            {
                _currentSource = value;
                RaisePropertyChanged(() => CurrentSource);
                OnCurrentSourceChanged();
            }
        }

        [Dependency]
        public IDialogService DialogService { get; set; }

        private void OnCurrentSourceChanged()
        {
            if (CurrentSource == null) return;

            if (CurrentSource.OriginalString.Contains(twitterSuccessfulShareUrl))
            {
                DialogService.ShowMessageBox("Tweet successfully posted.", "Hooray!", MessageBoxButton.OK);
                MessengerInstance.Send(new NavigateBackMessage());
            }
            else if (CurrentSource.OriginalString.Contains(facebookSuccessfulShareUrl))
            {
                DialogService.ShowMessageBox("Facebook link successfully posted.", "Hooray!", MessageBoxButton.OK);
                MessengerInstance.Send(new NavigateBackMessage());
            }
            else if (CurrentSource.OriginalString == twitterLoginFailUri)
            {
                DialogService.ShowMessageBox("Wrong twitter user name or password.", "Try again!", MessageBoxButton.OK);
                MessengerInstance.Send(new NavigateBackMessage());
                //MessengerInstance.Send(new NavigateWebBrowserToMessage(RealData));
            }
        }
    }
}